package reviewAlgorithms.dynamicProgramming;

import java.util.Arrays;
import java.util.Comparator;

public class CoinChange {
    public static void main(String[] args) {
        int amount = 6249;
        int[] coins = {186,419,83,408};
        System.out.println(coinChange(coins,amount));
        System.out.println(coinChange1(coins,amount));
    }

    private static int coinChange1(int[] coins, int amount) {
        if (coins == null || coins.length < 1) {
            return 0;
        }
        if (amount < 1) {
            return 0;
        }
        int[] dp = new int[amount + 1];
        Arrays.fill(dp,amount + 1);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < coins.length; j++) {
                if (i >= coins[j]){
                    dp[i] = Math.min(dp[i],dp[i - coins[j]] + 1);
                }
            }
        }
        return dp[amount] > amount ? -1:dp[amount];
    }

    public static int coinChange(int[] coins, int amount) {
        if (coins == null || coins.length < 1) {
            return 0;
        }
        if (amount < 1) {
            return 0;
        }
        int[][] dp = new int[coins.length][amount + 1];
        for (int[] item : dp) {
            Arrays.fill(item,amount + 1);
        }
        for (int i = 0; i < amount + 1; i++) {
            if (i % coins[0] == 0){
                dp[0][i] = i / coins[0];
            }
        }
        for (int i = 1; i < coins.length; i++) {
            for (int j = 0; j < amount + 1; j++) {
                if (j >= coins[i]){
                    dp[i][j] = Math.min(dp[i - 1][j],dp[i][j - coins[i]] + 1);
                }else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[coins.length - 1][amount] > amount?-1:dp[coins.length - 1][amount];
    }
}
